﻿using AspSQLProvider;
using Ext.Net;
using Ext.Net.MVC;
using PcaCore.BOL.Pca;
using PcaCore.DAL.Log;
using PcaCore.DAL.Pca;
using PcaWeb.Areas.Admin.Models;
using PcaWeb.Models;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace PcaWeb.Controllers
{
    [DirectController]
    public class HomeController : ControllerUser
    {
        public ActionResult GetModulos(string node)
        {
            //Por implementar usando base de datos
            if (node == "Root")
            {
                NodeCollection nodes = new NodeCollection(false);
                Node grupo;//Representa un grupo de modulos
                Node item;//Representa el modulo
                string url;

                #region Administracion

                string[] userRoles = Roles.GetRolesForUser();

                if (userRoles.Contains<string>("Administrador"))
                {
                    grupo = new Node();
                    grupo.NodeID = BaseControl.GenerateID();
                    grupo.Text = "Administracion";

                    url = Url.Action("Index", "Empresas", new { area = "Admin" });
                    item = new Node();
                    item.NodeID = "e" + Math.Abs(url.ToLower().GetHashCode());
                    item.Text = "Empresas";
                    item.Leaf = true;
                    item.Href = url;

                    grupo.Children.Add(item);

                    url = Url.Action("Index", "Oficinas", new { area = "Admin" });
                    item = new Node();
                    item.NodeID = "e" + Math.Abs(url.ToLower().GetHashCode());
                    item.Text = "Oficinas";
                    item.Leaf = true;
                    item.Href = url;

                    grupo.Children.Add(item);

                    url = Url.Action("Index", "Usuarios", new { area = "Admin" });
                    item = new Node();
                    item.NodeID = "e" + Math.Abs(url.ToLower().GetHashCode());
                    item.Text = "Usuarios";
                    item.Leaf = true;
                    item.Href = url;

                    grupo.Children.Add(item);

                    nodes.Add(grupo);
                }

                #endregion

                #region Grupo Pagos

                grupo = new Node();
                grupo.NodeID = BaseControl.GenerateID();
                grupo.Text = "Registro de pagos";

                url = Url.Action("Index", "GrupoMovimiento", new { area = "Caja" });
                item = new Node();
                item.NodeID = "e" + Math.Abs(url.ToLower().GetHashCode());
                item.Text = "Codigos";
                item.Leaf = true;
                item.Href = url;

                grupo.Children.Add(item);

                url = Url.Action("Index", "Registro", new { area = "Pagos" });
                item = new Node();
                item.NodeID = "e" + Math.Abs(url.ToLower().GetHashCode());
                item.Text = "Pagos";
                item.Leaf = true;
                item.Href = url;

                grupo.Children.Add(item);

                nodes.Add(grupo);

                #endregion

                if (userRoles.Contains<string>("Administrador"))
                {
                    #region Grupo Reportes

                    grupo = new Node();
                    grupo.NodeID = BaseControl.GenerateID();
                    grupo.Text = "Reportes";

                    url = Url.Action("RptRegistrosPorOficina", "Reportes", new { area = "Pagos" });
                    item = new Node();
                    item.NodeID = "e" + Math.Abs(url.ToLower().GetHashCode());
                    item.Text = "Consultas por oficina";
                    item.Leaf = true;
                    item.Href = url;

                    grupo.Children.Add(item);

                    nodes.Add(grupo);

                    #endregion
                }



                //#region Grupo Caja

                //grupo = new Node();
                //grupo.NodeID = BaseControl.GenerateID();
                //grupo.Text = "Caja";

                //url = Url.Action("Index", "LiquidacionUsuario", new { area = "Parqueos" });
                //item = new Node();
                //item.NodeID = "e" + Math.Abs(url.ToLower().GetHashCode());
                //item.Text = "Mis liquidaciones";
                //item.Leaf = true;
                //item.Href = url;

                //grupo.Children.Add(item);

                //url = Url.Action("Index", "MovimientoUsuario", new { area = "Parqueos" });
                //item = new Node();
                //item.NodeID = "e" + Math.Abs(url.ToLower().GetHashCode());
                //item.Text = "Mis movimientos";
                //item.Leaf = true;
                //item.Href = url;

                //grupo.Children.Add(item);

                //nodes.Add(grupo);

                //#endregion

                //#region Parqueo

                //grupo = new Node();
                //grupo.NodeID = BaseControl.GenerateID();
                //grupo.Text = "Parqueo";

                //url = Url.Action("Index", "RegistroActivo", new { area = "Parqueos" });
                //item = new Node();
                //item.NodeID = "e" + Math.Abs(url.ToLower().GetHashCode());
                //item.Text = "Registros activos";
                //item.Leaf = true;
                //item.Href = url;

                //grupo.Children.Add(item);

                //url = Url.Action("Index", "Cliente", new { area = "Socios" });
                //item = new Node();
                //item.NodeID = "e" + Math.Abs(url.ToLower().GetHashCode());
                //item.Text = "Clientes(abonados)";
                //item.Leaf = true;
                //item.Href = url;

                //grupo.Children.Add(item);

                //url = Url.Action("Index", "VehiculoAbonado", new { area = "Parqueos" });
                //item = new Node();
                //item.NodeID = "e" + Math.Abs(url.ToLower().GetHashCode());
                //item.Text = "Vehiculos abonados";
                //item.Leaf = true;
                //item.Href = url;

                //grupo.Children.Add(item);

                //url = Url.Action("Index", "PagoAbonado", new { area = "Parqueos" });
                //item = new Node();
                //item.NodeID = "e" + Math.Abs(url.ToLower().GetHashCode());
                //item.Text = "Pagos abonados";
                //item.Leaf = true;
                //item.Href = url;

                //grupo.Children.Add(item);

                //nodes.Add(grupo);

                //#endregion

                return this.Content(nodes.ToJson());
            }

            return new HttpStatusCodeResult((int)HttpStatusCode.BadRequest);
        }

        public ActionResult Index()
        {
            oEmpresa empresa = dEmpresa.Get(dSesion.GetEmpresaId());
            oOficina oficina = dOficina.Get(dSesion.GetEmpresaId(), dSesion.GetOficinaId());
            ViewBag.AppName = String.Format("{0} - {1}", empresa.nombre, oficina.nombre);
            PgMembershipUser user = new MembershipService().Get(this.HttpContext.User.Identity.Name);
            ViewBag.Username = (user != null) ? user.nick : "";
            return View();
        }

        [DirectMethod]
        [OutputCache(Duration = 86400, VaryByParam = "name")]
        public DirectResult GetHashCode(string name)
        {
            return this.Direct(Math.Abs(name.ToLower().GetHashCode()));
        }

        public ActionResult GetLicencia(string nombre, string fechaVencimiento)
        {
            bool isValid;
            int num;
            DateTime time;
            string licenseKey = String.Format("{0},3,{1}", nombre, fechaVencimiento);
            string[] strArray = licenseKey.Split(new char[] { ',' });
            if ((((strArray.Length == 3) && int.TryParse(strArray[1], out num)) && ((num >= 1) && DateTime.TryParseExact(strArray[2], "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out time))) && (time >= DateTime.Now))
                isValid = true;
            else
                isValid = false;

            licenseKey = Ext.Net.Utilities.StringUtils.Base64Encode(licenseKey);

            if (isValid)
                return Json(new { success = true, licencia = licenseKey }, JsonRequestBehavior.AllowGet);
            else
                return Json(new { success = false }, JsonRequestBehavior.AllowGet);
        }
    }
}
